package com.lego.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lego.bean.CommonResult;
import com.lego.service.impl.GoodsServiceImpl;
@WebServlet("/updateGoodsQuantity.do")
public class UpdateGoodsQuantityServlet extends HttpServlet{
	private GoodsServiceImpl gds = new GoodsServiceImpl();
	private ObjectMapper om = new ObjectMapper();
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		CommonResult result = new CommonResult();
	    
		
	    try {
            int goodsId = Integer.parseInt(req.getParameter("goodsId"));
            int rows = gds.updateGoodsQuantity(goodsId);
            
            if (rows == 1) {
                result.setCode(200);
            } else {
                result.setCode(500);
                result.setMessage("商品更新失败");
            }
        } catch (NumberFormatException e) {
            result.setCode(400);
            result.setMessage("商品ID格式错误");
        } catch (Exception e) {
            result.setCode(500);
            result.setMessage("服务器错误: " + e.getMessage());
        }
	     
	    resp.setContentType("application/json;charset=utf-8");
	    
	    resp.getWriter().write(om.writeValueAsString(result));

	}
}
